/*
 * @Author: Crism
 * @Date: 2018-11-21 14:40:34
 * @LastEditors: Crism
 * @LastEditTime: 2018-11-24 15:24:40
 * @Email: crism@qq.com
 * @Description: 
 */

const db_cms = require('../config').db_cms;
const db_user = require('../config').db_user;
const uuid = require('node-uuid');

/** 发表动态 */
module.exports.publish_post = publish_post;
async function publish_post(music_id, post_content, post_img, owner_id) {

    music_id = db_cms.escape(music_id);
    post_content = db_cms.escape(post_content);
    post_img = db_cms.escape(post_img);
    owner_id = db_cms.escape(owner_id);

    let sql = 'SELECT m.music_audio_id,m.music_cover_id,m.music_title,a.albums_name,a.owner_name FROM `music_info` m,albums_info a WHERE m.music_id=' + music_id + ' and m.music_status=0 and m.albums_id=a.albums_id';
    let music_info = await db_cms.queryOne(sql);
    if (music_info == null) {
        return [false, '音乐异常!'];
    }

    let post_id = "pst_" + uuid.v1().replace(/\-/g, '').substring(0, 28);
    let music_audio_id = music_info['music_audio_id'];
    let music_cover_id = music_info['music_cover_id'];
    let music_title = music_info['music_title'];
    let albums_name = music_info['albums_name'];
    let albums_owner = music_info['owner_name'];

    sql = 'INSERT INTO post_info(post_id,owner_id,post_content,post_img,music_id,music_title,music_cover_id,music_audio_id,albums_name,albums_owner,create_time) VALUE("' + post_id + '",' + owner_id + ',' + post_content + ',' + post_img + ',' + music_id + ',"' + music_title + '",' + music_cover_id + ',' + music_audio_id + ',"' + albums_name + '","' + albums_owner + '",UNIX_TIMESTAMP(NOW()))';
    let flag = await db_cms.queryNum(sql);
    return [flag > 0, flag > 0 ? '发布成功!' : '发布失败!'];
}

/** 删除动态 */
module.exports.del_post = del_post;
async function del_post(post_id, owner_id) {
    post_id = db_cms.escape(post_id);
    owner_id = db_cms.escape(owner_id);
    let sql = 'DELETE FROM `post_info` WHERE post_id=' + post_id + ' and owner_id=' + owner_id;
    let flag = await db_cms.queryNum(sql);
    return [flag > 0, flag > 0 ? '操作成功' : '操作失败'];
}

/** 获取动态列表 */
module.exports.get_posts_list = get_posts_list;
async function get_posts_list(owner_id, begin, plen) {
    owner_id = db_cms.escape(owner_id);
    let sql = 'SELECT c.comment_id,c.comment_content,c.commenter_img,c.item_id music_id,m.music_title,m.music_cover_id,m.music_audio_id,a.albums_name,a.owner_name albums_owner,c.comment_time FROM `comments` c,`music_info` m,albums_info a WHERE c.item_type=1 and c.commenter_id=' + owner_id + ' AND m.music_id=c.item_id and m.music_status=0 and m.albums_id=a.albums_id ORDER BY comment_time DESC Limit ' + begin + ',' + plen;
    let post_list = await db_cms.query(sql);
    if (post_list instanceof Array && post_list.length > 0) {
        return post_list;
    }
    return [];
}

/** 获取用户评论音乐的总数 */
module.exports.get_posts_total = get_posts_total;
async function get_posts_total(owner_id) {
    owner_id = db_cms.escape(owner_id);
    let sql = 'SELECT COUNT(comment_id) FROM `comments` WHERE item_type=1 and commenter_id=' + owner_id;
    let post_total = await db_cms.queryOne(sql);
    return post_total != null ? post_total['num'] : 0;
}
